package com.app.mingluxing.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.RegisterToken;
import com.app.mingluxing.bean.ResultBaseBean;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 忘记密码页面
 * Created by Aaron on 15/8/3.
 */
public class ForgetPswActivity extends BaseActivity {

    private EditText et_tel;
    private EditText et_verify_code;
    private TextView tv_get_verify_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_forget_psw);

        initWidget();
    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_get_verify_code = (TextView) findViewById(R.id.tv_get_verify_code);

        tv_title.setText(getResources().getString(R.string.forget_psw_title));

        et_tel = (EditText) findViewById(R.id.et_photo);
        et_verify_code = (EditText) findViewById(R.id.et_verify_code);

        Button btn_commit = (Button) findViewById(R.id.btn_commit);

        iv_left.setOnClickListener(this);
        tv_get_verify_code.setOnClickListener(this);
        btn_commit.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;
            case R.id.tv_get_verify_code: //获取验证码

                if (CommonUtils.isNull(et_tel.getText().toString().trim())) {
                    showToast("请输入手机号码");
                    return;
                }

                //倒计时
                countDown();

                //获取验证码
                getSms();

                break;
            case R.id.btn_commit: //下一步

                commuit();

                break;

        }

    }

    /**
     * 提交
     */
    private void commuit() {

        if (CommonUtils.isNull(et_tel.getText().toString().trim())) {
            showToast("请输入手机号码");
            return;
        }

        if (CommonUtils.isNull(et_verify_code.getText().toString().trim())) {
            showToast("请输入验证码");
            return;
        }

//        Intent intent = new Intent(this, RegisterInputActivity.class);
//        //type 0 是注册 输入密码  1 是忘记密码 重置密码
//        intent.putExtra("type",1);
//        startActivity(intent);
        //提交验证
        submitConfirm();

    }

    /**
     * 提交验证码
     */
    private void submitConfirm() {

        JSONObject j = new JSONObject();
        j.put("phoneNum", et_tel.getText().toString().trim());
        j.put("msCode", et_verify_code.getText().toString().trim());

        HttpUtil.setPostRequest("验证中...", this, Constant.WEB_ROOT + Constant.FORGETCHECK,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            RegisterToken resultBaseBean = JSONObject.parseObject(str, RegisterToken.class);
                            if (Constant.STATUS_SUCCESS == resultBaseBean.code) {

                                Intent intent = new Intent(ForgetPswActivity.this, ForgetInputPswActivity.class);
                                intent.putExtra("token", resultBaseBean.token.token);
                                startActivity(intent);

                            } else {
                                showToast(Constant.showErrorMsg(resultBaseBean.code, ForgetPswActivity.this));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast("验证失败");
                        }

                    }
                }, null);

    }

    /**
     * 获取验证码
     */
    private void getSms() {

        JSONObject j = new JSONObject();
        j.put("phoneNum", et_tel.getText().toString().trim());
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("type", "1");//0:注册,1:密码验证,2:变更验证,3:身份验证

        HttpUtil.setPostRequest("正在获取验证码", this, Constant.WEB_ROOT + Constant.SMSCODE,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            ResultBaseBean resultBaseBean = JSONObject.parseObject(str, ResultBaseBean.class);

                            if (resultBaseBean.code == Constant.STATUS_SUCCESS) {

                                showToast("请注意查收验证码");

                            } else {
                                showToast(Constant.showErrorMsg(resultBaseBean.code, ForgetPswActivity.this));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();

                        }

                    }
                }, null);

    }

    protected void onDestroy() {
        super.onDestroy();

//        if (ready) {
//            // 销毁回调监听接口
//            SMSSDK.unregisterAllEventHandler();
//        }
        if (executor != null) {
            executor.shutdownNow();
            executor = null;
        }
    }

    /**
     * 倒计时
     */
    private void countDown() {

        tv_get_verify_code.setClickable(false);
        tv_get_verify_code.setEnabled(false);
        if (executor == null) {
            executor = Executors.newSingleThreadExecutor();
        }

        tv_get_verify_code.setText(currentTime + " 秒");
        for (int i = 0; i < 60; i++) {

            executor.execute(new Runnable() {

                @Override
                public void run() {

                    SystemClock.sleep(1000);

                    changeTime();
                }
            });

        }

    }

    int currentTime = 60;
    private ExecutorService executor;

    /**
     * 改变界面时间
     */
    private void changeTime() {
        runOnUiThread(new Runnable() {
            public void run() {

                if (currentTime == 1) {
                    currentTime = 60;
                    tv_get_verify_code.setText(getResources().getString(R.string.get_verify_code));
                    tv_get_verify_code.setClickable(true);
                    tv_get_verify_code.setEnabled(true);

                } else {
                    tv_get_verify_code.setText(--currentTime + " 秒");
                }
            }
        });
    }

}
